#![no_std]
#![no_main]

use bootloader::{entry_point, BootInfo};
use core::panic::PanicInfo;
use uart_16550::SerialPort;

entry_point!(kernel_main);

fn kernel_main(_boot_info: &'static BootInfo) -> ! {
    let message = "Hello from Rust OS!";
    write_string_to_vga(message);
    write_string_to_serial(message);

    loop {}
}

fn write_string_to_vga(message: &str) {
    let buffer_ptr = 0xb8000 as *mut u8;
    for (i, byte) in message.bytes().enumerate() {
        unsafe {
            *buffer_ptr.add(i * 2) = byte;
            *buffer_ptr.add(i * 2 + 1) = 0x0f;
        }
    }
}

fn write_string_to_serial(message: &str) {
    let mut serial_port = unsafe { SerialPort::new(0x3F8) };
    serial_port.init();
    for byte in message.bytes() {
        serial_port.send(byte);
    }
    serial_port.send(b'\n');
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}
